package com.sojson.util.ali;

import java.io.IOException;
import java.util.Map;

import com.sojson.util.SystemParamBaseUtil;
import com.sojson.util.ali.bean.AliConfig;
import com.sojson.util.ali.constant.AliConstant;

/**
 * 阿里工具类
 * 
 * @author liu
 * @date 2021-07-15
 */
public class AliUtil {

    /**
     * 获取阿里配置参数
     * 
     * @return
     * @throws IOException 
     */
    public static AliConfig getConfig() throws IOException {
        AliConfig config = new AliConfig();
        Map<String, String> map = getMap();
        config.setAccessKey(map.get(AliConstant.CACHE_ACCESS_KEY));
        config.setAccessSecret(map.get(AliConstant.CACHE_ACCESS_SECRET));
        return config;
    }

    /**
     * 获取阿里配置参数
     * 
     * @param map   缓存的Map
     * @return
     * @throws IOException
     */
    public static AliConfig getConfig(Map<String, String> map) throws IOException {
        AliConfig config = new AliConfig();
        config.setAccessKey(map.get(AliConstant.CACHE_ACCESS_KEY));
        config.setAccessSecret(map.get(AliConstant.CACHE_ACCESS_SECRET));
        return config;
    }

    /**
     * 获取阿里短信配置参数
     * 
     * @return
     * @throws IOException 
     */
    public static AliConfig getConfigSms() throws IOException {
        Map<String, String> map = getMap();
        AliConfig config = getConfig(map);
        config.setSmsSign(map.get(AliConstant.CACHE_SMS_SIGN));
        config.setSmsTemplateCodeVerifyCode(map.get(AliConstant.CACHE_SMS_TEMPLATE_CODE_VERIFYCODE));
        config.setSmsTemplateCodeGoods(map.get(AliConstant.CACHE_SMS_TEMPLATE_CODE_GOODS));
        config.setSmsTemplateCodeHotel(map.get(AliConstant.CACHE_SMS_TEMPLATE_CODE_HOTEL));
        config.setSmsTemplateCodePost(map.get(AliConstant.CACHE_SMS_TEMPLATE_CODE_POST));
        return config;
    }

    /**
     * 获取阿里OSS配置参数
     * 
     * @return
     * @throws IOException 
     */
    public static AliConfig getConfigOss() throws IOException {
        Map<String, String> map = getMap();
        AliConfig config = getConfig(map);
        config.setRole(map.get(AliConstant.CACHE_ROLE));
        config.setSessionName(map.get(AliConstant.CACHE_SESSION_NAME));
        return config;
    }

    /** 获取 AccessKey */
    public static String getAccessKey() throws IOException {
        return get(AliConstant.CACHE_ACCESS_KEY);
    }

    /** 获取 AccessSecret */
    public static String getAccessSecret() throws IOException {
        return get(AliConstant.CACHE_ACCESS_SECRET);
    }

    /** 获取 角色 */
    public static String getRole() throws IOException {
        return get(AliConstant.CACHE_ROLE);
    }

    /** 获取 SessionName */
    public static String getSessionName() throws IOException {
        return get(AliConstant.CACHE_SESSION_NAME);
    }

    /** 获取 短信签名 */
    public static String getSmsSign() throws IOException {
        return get(AliConstant.CACHE_SMS_SIGN);
    }

    /** 获取 验证码短信模板Code */
    public static String getSmsTemplateCodeVerifyCode() throws IOException {
        return get(AliConstant.CACHE_SMS_TEMPLATE_CODE_VERIFYCODE);
    }

    /** 获取 商城下单短信模板Code */
    public static String getSmsTemplateCodeGoods() throws IOException {
        return get(AliConstant.CACHE_SMS_TEMPLATE_CODE_GOODS);
    }

    /** 获取 酒店下单短信模板Code */
    public static String getSmsTemplateCodeHotel() throws IOException {
        return get(AliConstant.CACHE_SMS_TEMPLATE_CODE_HOTEL);
    }

    /** 获取 职位应聘短信模板Code */
    public static String getSmsTemplateCodePost() throws IOException {
        return get(AliConstant.CACHE_SMS_TEMPLATE_CODE_POST);
    }

    /**
     * 从缓存中获取值
     * 
     * @return
     * @throws IOException
     */
    private static Map<String, String> getMap() throws IOException {
        return SystemParamBaseUtil.gets();
    }

    /**
     * 从缓存中获取值
     * 
     * @param key
     * @return
     * @throws IOException
     */
    private static String get(String key) throws IOException {
        return SystemParamBaseUtil.get(key);
    }

}